import sys
import time
import pytest

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from DButils import select
from DButils import update

from common1.register_readdata import get_data
from common1.register_operation import *

class Testregister():
    @pytest.fixture(scope="function", autouse=True)
    def setup_teardown(self):
        service = Service("chromedriver.exe")
        self.driver = webdriver.Chrome(service=service)
        self.driver.get("http://localhost:90/toregister.html")
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
        yield
        self.driver.quit()

    #注册成功
    @pytest.mark.parametrize("username, password, r_password, expect", get_data(0))
    def test_register_normal(self, username, password, r_password, expect):
        fill_form(self.driver, username, password, r_password)

        result = self.driver.title
        assert expect == result

        sql = "select * from user where username  = %s"
        param = [username]
        expect = len(select(sql, param))
        assert expect == 1

        sql = "delete from user where username  = %s"
        param = [username]
        update(sql, param)

    #注册失败-重复用户
    @pytest.mark.parametrize("username, password, r_password, expect", get_data(1))
    def test_register_fail(self, username, password, r_password, expect):
        fill_form(self.driver, username, password, r_password)

        result = self.driver.find_element(By.XPATH, "/html/body/div/div/div/form/div[1]/span[1]").text
        assert expect == result

    #两次密码不相同
    @pytest.mark.parametrize("username, password, r_password, expect", get_data(2))
    def test_password_error(self, username, password, r_password, expect):
        fill_form(self.driver, username, password, r_password)

        result = self.driver.find_element(By.XPATH, "/html/body/div/div/div/form/div[3]/span[1]").text
        assert expect == result
        #测试不通过

    #缺少确认密码
    @pytest.mark.parametrize("username, password, r_password, expect", get_data(3))
    def test_r_password_error(self, username, password, r_password, expect):
        fill_form(self.driver, username, password, r_password)

        result = self.driver.find_element(By.XPATH, "/html/body/div/div/div/form/div[3]/span[1]").text
        assert expect == result

    #密码为空
    @pytest.mark.parametrize("username, password, r_password, expect", get_data(4))
    def test_z_password_error(self, username, password, r_password, expect):
        fill_form(self.driver, username, password, r_password)

        result = self.driver.find_element(By.XPATH, "/html/body/div/div/div/form/div[2]/span[1]").text
        assert expect == result

    #管理员注册
    @pytest.mark.parametrize("username, password, r_password, expect", get_data(5))
    def test_admin(self, username, password, r_password, expect):
        fill_form(self.driver, username, password, r_password)

        #检查amdin数据库
        sql = "select * from admin where username  = %s"
        param = ["admin"]
        expect = len(select(sql, param))
        assert expect == 1

        sql = "delete from user where username  = %s"
        param = [username]
        update(sql, param)

    #用户名为空
    @pytest.mark.parametrize("username, password, r_password, expect", get_data(6))
    def test_none(self, username, password, r_password, expect):
        fill_form(self.driver, username, password, r_password)

        result = self.driver.find_element(By.XPATH, "/html/body/div/div/div/form/div[1]/span[1]").text
        assert expect == result

    #用户名输入21个字符
    @pytest.mark.parametrize("username, password, r_password, expect", get_data(7))
    def test_fail_m(self, username, password, r_password, expect):
        fill_form(self.driver, username, password, r_password)

        sql = "select * from user where username  = %s"
        param = ["abcabcabcabcabcabcabc"]
        expect = len(select(sql, param))
        assert expect == 0

















